/**
 * Created by xuan on 7/10/15.
 */
define(function (require, exports, module) {

    var $ = require('jquery');

    require('./dragsort');

    var angular = require('angular');

    var dragsort = angular.module('directives.dragsort',[]);
    dragsort.directive('dragSort',[
        '$compile',
        function ($compile) {
            return {
                restrict : "A",
                link: function (scope, element, attr) {
                    var dragEndFn = attr.dragSortFn?scope.$eval(attr.dragSortFn):function(){};
                    var dragSelector = attr.dragSelector;
                    var dragSelectorExclude = attr.dragSelectorExclude;
                    var placeHolderTemplate = attr.placeHolderTemplate || '<li></li>';
                    var $element= $(element);
                    scope.$watch(attr.dragSort,function(newvalue){
                        if(newvalue){
                            $element.dragsort({
                                //itemSelector : dragSelector,
                                dragSelector: dragSelector,
                                dragSelectorExclude : dragSelectorExclude,
                                dragBetween: true,
                                placeHolderTemplate:placeHolderTemplate,
                                dragEnd:dragEndFn
                            });
                        }else{
                            $element.dragsort('destroy');
                        }
                    })
                }
            };
        }
    ])
});